﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProjectDirectory_project_directory : System.Web.UI.Page
{
    public int PageSize = 10;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["PageIndex"] = 1;
            BindData();
        }

        // 手动注册事件
        string eventTarget = Request["__EVENTTARGET"];
        if (eventTarget == "btnPrev")
        {
            btnPrev_Click(null, null);
        }
        else if (eventTarget == "btnNext")
        {
            btnNext_Click(null, null);
        }
    }

    private int CurrentPage
    {
        get { return (int)(ViewState["PageIndex"] ?? 1); }
        set { ViewState["PageIndex"] = value; }
    }

    private void BindData()
    {
        string regionVal = region.SelectedValue;
        string categoryVal = category.SelectedValue;
        string publishTime = status.SelectedValue;
        string keyword = searchInput.Text.Trim();

        List<CQFY.Model.projectDirectory> allData = CQFY.BLL.projectDirectory.ListByCondition(regionVal, categoryVal, publishTime, keyword);

        int totalCount = allData.Count;
        int totalPages = (int)Math.Ceiling((double)totalCount / PageSize);

        if (CurrentPage > totalPages && totalPages > 0)
        {
            CurrentPage = totalPages;
        }

        int startIndex = (CurrentPage - 1) * PageSize;
        var pagedData = allData.Skip(startIndex).Take(PageSize).ToList();

        if (pagedData.Count == 0)
        {
            repeaterInheritors.DataSource = null;
            repeaterInheritors.DataBind();

            ltNoData.Text = "<tr><td colspan='7' style='text-align:center;'>暂无数据</td></tr>";
        }
        else
        {
            repeaterInheritors.DataSource = pagedData;
            repeaterInheritors.DataBind();
            ltNoData.Text = "";
        }


        lblPageInfo.Text = $"第 {CurrentPage} 页 / 共 {totalPages} 页";
        btnPrev.Attributes["class"] = CurrentPage > 1 ? "pre-page" : "pre-page disabled";
        btnNext.Attributes["class"] = CurrentPage < totalPages ? "next-page" : "next-page disabled";
    }


    protected void btnPrev_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            BindData();
        }
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        BindData();
    }

    protected void searchBtn_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        BindData();
    }
}